/**
 * <p>Copyright (c) 2011 深圳市鹏途信息技术有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	东莞交通工程质量监督综合业务系统        </p>
 * <p>创建者   :	Administrator 
 * 
 * <p>描   述  :   ArchiveAction.java for com.pengtu.web.archive    </p>
 * 
 * <p>最后修改 : $: 2011-11-22-下午4:12:16 v 1.0.0	 Administrator   $     </p>
 * 
*/

package com.pengtu.web.archive;

import java.io.File;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.Constants;
import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.dao.QueryFilter.MatchType;
import com.pengtu.entity.app.Attachment;
import com.pengtu.service.app.SystemManager;
import com.pengtu.service.project.ProjectManager;
import com.pengtu.utils.DateUtils;
import com.pengtu.utils.FileUtils;
import com.pengtu.utils.UploadFile;
import com.pengtu.utils.ZipHelper;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

/**
 * 
 * ArchiveAction
 * 
 * 2011-11-22 下午4:12:16
 * 
 * @version 1.0.0
 * 
 */
public class ArchiveAction extends BaseActionSupport<Attachment>{

	/**
	 * serialVersionUID:TODO
	 *
	 * @since 1.0.0
	 */
	
	private static final long serialVersionUID = 1L;
	private SystemManager systemManager;
	private ProjectManager projectManager;

	// -- 页面属性 --//
	private String id;
	private String partId; // 对应与某一部分
	private String tableId; // 对应表id
	private String tableName; // 对应某表
	private String tableField;	// 对应表中的某个字段
	private File upfile; // 文件
	private String upfileContentType; // 文件类型
	private String upfileFileName; // 文件原始名
	private Attachment attachment;
	private String fileTimee;
	private String fileTimes;
	
	private Page<Attachment> page = new Page<Attachment>(20);
	@Override
	public Attachment getModel() {
		return attachment;
	}

	@Override
	public String list() throws Exception {
		QueryFilter filters = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("tableName");
			page.setOrder(Page.DESC);
		}
		filters.addFilter("partId", partId, MatchType.EQ);
		filters.addFilter("tableField", tableField, MatchType.LIKE);
		filters.addFilter("upfileFileName", upfileFileName, MatchType.LIKE);
		filters.addFilter("fileTime", DateUtils.convertStringToStrigulaDate(fileTimes), MatchType.GE);
		filters.addFilter("fileTime", DateUtils.convertStringToStrigulaDate(fileTimee), MatchType.LE);
		page = systemManager.getAttachmentList(page, filters);
		return SUCCESS;
	}
	public String pro() throws Exception {
		return "pro";
	}
	@Override
	public String input() throws Exception {
		return null;
	}

	@Override
	public String save() throws Exception {
		return null;
	}

	@Override
	public String delete() throws Exception {
		systemManager.deleteAttachment(Struts2Utils.getAppRootPath(),id);
		Struts2Utils.renderJson(Constants.EXT_SUCCESS_JSON_STR);
		return NONE;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			attachment = systemManager.getAttachment(id);
		} else {
			attachment = new Attachment();
		}
	}
	
	public void downloadMany() throws Exception {
		String examplePath = Struts2Utils.getAppRootPath() + "\\resources\\archives.xls";
		List<Attachment> attachments = systemManager.getAttachmentLists(partId, tableName, tableField, upfileFileName);
		Long currentDate = System.currentTimeMillis();
		String toFilename = System.getProperty("java.io.tmpdir")+currentDate + Constants.FILE_SEP;
		String toFile = toFilename+currentDate + Constants.FILE_SEP;
		(new File(toFile)).mkdirs();
		for(int i = 0 ; i < attachments.size() ; i++){
			Attachment attachment = attachments.get(i);
			String fromFile = Struts2Utils.getAppRootPath()+Constants.FILE_SEP + attachment.getFullPath();
			FileUtils.copy(fromFile, toFile + attachment.getUpfileFileName(), false);
		}
		String projName = projectManager.getProj("id", partId).getProjName();
		systemManager.excelItem(attachments,toFile+projName+"业务资料列表.xls",examplePath);
        String a  = currentDate+".zip";
		ZipHelper.getZipHelper().zip(toFile, toFilename+a);
		UploadFile.downloadFileByInputStream(Struts2Utils.getRequest(), Struts2Utils.getResponse(), new File(toFilename+a));
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPartId() {
		return partId;
	}

	public void setPartId(String partId) {
		this.partId = partId;
	}

	public String getTableId() {
		return tableId;
	}

	public void setTableId(String tableId) {
		this.tableId = tableId;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getTableField() {
		return tableField;
	}

	public void setTableField(String tableField) {
		this.tableField = tableField;
	}

	public File getUpfile() {
		return upfile;
	}

	public void setUpfile(File upfile) {
		this.upfile = upfile;
	}

	public String getUpfileContentType() {
		return upfileContentType;
	}

	public void setUpfileContentType(String upfileContentType) {
		this.upfileContentType = upfileContentType;
	}

	public String getUpfileFileName() {
		return upfileFileName;
	}

	public void setUpfileFileName(String upfileFileName) {
		this.upfileFileName = upfileFileName;
	}

	public Page<Attachment> getPage() {
		return page;
	}

	public void setPage(Page<Attachment> page) {
		this.page = page;
	}

	public String getFileTimee() {
		return fileTimee;
	}

	public void setFileTimee(String fileTimee) {
		this.fileTimee = fileTimee;
	}

	public String getFileTimes() {
		return fileTimes;
	}

	public void setFileTimes(String fileTimes) {
		this.fileTimes = fileTimes;
	}

	@Autowired
	public void setSystemManager(SystemManager systemManager) {
		this.systemManager = systemManager;
	}

	@Autowired
	public void setProjectManager(ProjectManager projectManager) {
		this.projectManager = projectManager;
	}

}
